package cn.wp.common.core.plugin.verification;

import cn.wp.common.core.Wp;
import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.exception.WpException;
import cn.wp.common.core.plugin.email.MailService;
import cn.wp.common.core.web.annotation.ClearInterceptor;
import cn.wp.common.core.web.interceptor.LoginInterceptor;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码服务：包括验证码生成和图片验证码
 * Created by chenyb on 2019/10/18
 */
@Controller
@RequestMapping(value = Wp.WebCommonBase + "/vcode")
@ClearInterceptor(LoginInterceptor.class)
@Slf4j
public class VerificationCodeController {

    private Producer captchaProducer = null;

    @Autowired
    private VerificationCodeService verificationCodeService;

    @Autowired
    private MailService mailService;

    @Autowired
    public void setCaptchaProducer(Producer captchaProducer) {
        this.captchaProducer = captchaProducer;
    }

    /**
     *
     * 获取验证码图片
     *
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("img")
    public ModelAndView getCaptchaCode(String randomStr, HttpServletResponse response){
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        // 生成验证码文本
        String capText = captchaProducer.createText();

        // 存储验证码
        verificationCodeService.saveCode(randomStr,capText);

        // 利用生成的字符串构建图片
        try{
            BufferedImage bi = captchaProducer.createImage(capText);
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(bi, "jpeg", out);
            try {
                out.flush();
            } finally {
                out.close();
            }
        }catch (IOException e){
            throw new WpException("生成图片验证码出错",e);
        }
        return null;
    }

    /**
     *
     * 发送邮箱验证码
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("sendEmailCode")
    @ResponseBody
    public WpResult sendEmailCode(String randomStr,String email) {
        String code = verificationCodeService.genCode(randomStr,6);
        log.info("send verificationCode to email:" + email + ",code = " + code);
        mailService.sendMime(email,"验证码",code);
        log.info("send verificationCode to email:" + email + " success");
        return new WpResult().success();
    }


}
